package com.lys.enchantment.weapon;

import net.minecraft.client.gui.screen.Screen;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.entity.*;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.AxeItem;
import net.minecraft.item.ItemStack;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.random.Random;

import java.util.List;

public class LightningAxeEnchantment extends Enchantment {

    public static final String LIGHTNING_AXE_TIP_TRANSLATION_KEY = "lightning_axe_tip_translation_key";

    public LightningAxeEnchantment(Rarity weight, EnchantmentTarget target, EquipmentSlot... slotTypes) {
        super(weight, target, slotTypes);
    }

    @Override
    public boolean isAcceptableItem(ItemStack stack) {
        return stack.getItem() instanceof AxeItem;
    }

    @Override
    public int getMaxLevel() {
        return 3;
    }

    @Override
    public float getAttackDamage(int level, EntityGroup group) {
        return 1.0f + (float)Math.max(0, level - 1) * 0.7f;
    }

    @Override
    public void onTargetDamaged(LivingEntity user, Entity target, int level) {
        if (user.getWorld() instanceof ServerWorld world) {
            LightningEntity lightningEntity = EntityType.LIGHTNING_BOLT.create(world);
            if (lightningEntity == null) {
                return;
            }
            lightningEntity.refreshPositionAfterTeleport(target.getX(), target.getY(), target.getZ());
            lightningEntity.setCosmetic(false);
            world.spawnEntity(lightningEntity);
            Round((PlayerEntity)user, 10, 5);
        }
    }

    public static void Round(PlayerEntity player, int particleCount, int radius) {
        ServerWorld world = (ServerWorld) player.getEntityWorld();
        Random random = world.getRandom();

        for (int i = 0; i < particleCount; i++) {
            LightningEntity lightningEntity = EntityType.LIGHTNING_BOLT.create(world);
            assert lightningEntity != null;

            lightningEntity.setCosmetic(false);
            double angle = random.nextDouble() * 2 * Math.PI; // 随机角度
            double x = player.getX() + radius * Math.cos(angle); // 根据角度计算 x 坐标
            double y = player.getY() + random.nextDouble() * 0.5; // 随机高度
            double z = player.getZ() + radius * Math.sin(angle); // 根据角度计算 z 坐标
            lightningEntity.refreshPositionAfterTeleport(x, y, z);
            world.spawnEntity(lightningEntity);

        }
    }

    public static void appendTooltip(List<Text> tooltip) {
        if (Screen.hasShiftDown()) {
            tooltip.add(Text.translatable("enchantment.moreenchantmentmod.lightning_axe").formatted(Formatting.RED));
            tooltip.add(Text.translatable(LIGHTNING_AXE_TIP_TRANSLATION_KEY).formatted(Formatting.BLUE));
        } else {
            tooltip.add(Text.translatable("tip_translation_key").formatted(Formatting.GRAY));
        }
    }
}
